package academic.nantes.teamsync;

import java.util.Iterator;

import academic.nantes.teamsync.DAO.TeamSyncLivrable;
import academic.nantes.teamsync.DAO.TeamSyncProject;
import academic.nantes.teamsync.DAO.TeamSyncTache;
import academic.nantes.teamsync.DAO.TeamSyncUser;
import academic.nantes.teamsync.customwidget.TeamSyncTable;
import academic.nantes.teamsync.customwidget.TeamSyncTextView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableRow;

public class Planning extends Activity {

	TeamSyncTable tablePrevi;
	TeamSyncTable tableLiv;
	private TableRow rowPrevi;
	private TeamSyncTextView tvprevi1;
	private TeamSyncTextView tvprevi2;
	private TeamSyncTextView tvprevi3;
	private TeamSyncTextView tvprevi4;
	private TeamSyncTextView tvprevi5;
	private TeamSyncTextView tvprevi6;
	private TableRow rowLiv;
	private TeamSyncTextView tvliv1;
	private TeamSyncTextView tvliv2;
	private TeamSyncTextView tvliv3;
	private TeamSyncTextView tvliv4;
	private TeamSyncTextView tvliv5;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.planning);

		//
		loadGridPrevi();
		//Livrables du projet
		loadGridLiv();
	}


	private void loadGridPrevi() {
		// TODO Auto-generated method stub

		tablePrevi = (TeamSyncTable) findViewById(R.id.tablePrevi);
		for (TeamSyncTache t : TeamSyncProject.getInstance().getTaches()) {
			rowPrevi = new TableRow(this);
			rowPrevi.setOrientation(TableRow.VERTICAL);

			tvprevi1 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvprevi2 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvprevi3 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvprevi4 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvprevi5 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvprevi6 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);

			tvprevi1.setText(t.getLibelle());
			tvprevi2.setText(t.getDateDebTheoriqueToStringFormattedAs("dd/MM"));
			tvprevi3.setText(t.getEtat());
			tvprevi4.setText(t.getDateFinTh("dd/MM"));

			String strRess = "";

			int idT = t.getId();
			Iterator<Integer> iter = TeamSyncProject.getInstance().getRealisations().getUtilisateurs(idT).iterator();
			
			while(iter.hasNext()){
				Integer u = iter.next();
				TeamSyncUser user = TeamSyncProject.getInstance().getMembreEquipe(u.intValue());
				if (null != user)
					strRess += user.getInitials() + "\n";
			}
			tvprevi5.setText(strRess);

			TeamSyncLivrable l = TeamSyncProject.getInstance().getLivrableById(t.getLivrable());
			if (null == l)
				tvprevi6.setText("<inconnu>");
			else
				tvprevi6.setText(l.getNomLivrable());

			tablePrevi.addcustomViewToRow(rowPrevi, tvprevi1);
			tablePrevi.addcustomViewToRow(rowPrevi, tvprevi2);
			tablePrevi.addcustomViewToRow(rowPrevi, tvprevi3);
			tablePrevi.addcustomViewToRow(rowPrevi, tvprevi4);
			tablePrevi.addcustomViewToRow(rowPrevi, tvprevi5);
			tablePrevi.addcustomViewToRow(rowPrevi, tvprevi6);

			tablePrevi.addCustomRow(rowPrevi);
		}
	}


	private void loadGridLiv() {

		TeamSyncProject monProj = TeamSyncProject.getInstance();

		tableLiv = (TeamSyncTable) findViewById(R.id.tableLivProj);
		for (TeamSyncLivrable l : monProj.getLivrables()) {
			rowLiv = new TableRow(this);
			rowLiv.setOrientation(TableRow.VERTICAL);

			tvliv1 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvliv2 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvliv3 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvliv4 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
			tvliv5 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);

			tvliv1.setText(l.getNomLivrable());
			tvliv2.setText(l.getDateDebToStringFormattedAs("MMM/yy"));
			tvliv3.setText(l.getDateFinToStringFormattedAs("MMM/yy"));
			tvliv4.setText(l.getTachesReal());
			TeamSyncUser responsable = monProj.getMembreEquipe(l.getIdResp());
			if (null != responsable)
				tvliv5.setText(responsable.getInitials());
			else
				tvliv5.setText("<inconnu>");

			tableLiv.addcustomViewToRow(rowLiv, tvliv1);
			tableLiv.addcustomViewToRow(rowLiv, tvliv2);
			tableLiv.addcustomViewToRow(rowLiv, tvliv3);
			tableLiv.addcustomViewToRow(rowLiv, tvliv4);
			tableLiv.addcustomViewToRow(rowLiv, tvliv5);

			tableLiv.addCustomRow(rowLiv);
		}
	}

}
